package com.aimee.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.aimee.dao.LoginDAO;
import com.aimee.entity.User;
import com.aimee.service.UserRoleService;

/**
 * 用户登录
 */
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public LoginServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	LoginDAO dao = new LoginDAO();
	UserRoleService userService = new UserRoleService();

	// 客户端是get请求
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession();
		response.setContentType("text/html;charset=UTF-8");
		//从session 里取出验证码
		String yzm0 = (String) session.getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
		System.out.println(yzm0);
		// 登录才可进入个人中心
		// 接受请求 request
		int user_id =Integer.parseInt(request.getParameter("user_id"));
		String user_password = request.getParameter("user_password");
		String yz = request.getParameter("yz");
		
		System.out.println(yz);
		User user = null;
		// 验证码判断 yzm0.equalsIgnoreCase(yz)
		if (yzm0.equalsIgnoreCase(yz)) {
			try {
				user = dao.login(user_id, user_password);
//				System.out.println(user.getUser_id());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		
		
		if (user == null) {
			// request 里放点错误消息
			request.setAttribute("loginerr", "用户名或密码错误");
			System.out.println("用户名或密码错误");
			request.getRequestDispatcher("/login.html").forward(request, response);
//			response.sendRedirect("/OnlineExam/login.html");
		} else {
			System.out.println(user.getUser_id());
			session.setAttribute("user_id",user.getUser_id());
			//获取角色id
			try {
				User role=dao.findroleid(user_id);
				int role_id=role.getRole_id();
				//页面权限判断
//				List pageList = userService.getPagesByRoleId(role_id);
//				session.setAttribute("pageList", pageList);
//判断跳转界面
				if (role_id == 1) {
//				response.sendRedirect("/OnlineExam/admin/homepage.jsp");
//				session.setAttribute("user_id", user_id);
//				request.getRequestDispatcher("/admin/homepage.jsp").forward(request, response);
					response.sendRedirect("/OnlineExam/admin/homepage.jsp");
//				response.sendRedirect("/OnlineExam/admin/student?m=page");
				} else {
					response.sendRedirect("/OnlineExam/user/homepage.jsp");
				}


			} catch (SQLException e) {
				e.printStackTrace();
			}
			

		}
		
		
		
		out.flush();
		out.close();

	}

	// 客户端是post请求
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
